﻿Public Class Form1

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub
    Sub Carga_Cantidad(ByVal familia As Integer)
        Dim exec As New DotNetDal.SQLPersistence(False, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
        Dim rp As New DotNetResponse.SQLPersistence
        rp.StoredProcedure = "SP_GET_CANTIDAD"
        rp.AddParametro(familia)
        exec.ExecuteDataTable(rp)

        cmb_cantidad.DisplayMember = "DESCRIPCION_CANTIDAD"
        cmb_cantidad.ValueMember = "CANTIDAD"
        cmb_cantidad.DataSource = rp.DtTable

    End Sub

    Sub Get_Datos_Producto(ByVal Id As Integer)
        Dim exec As New DotNetDal.SQLPersistence(False, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
        Dim rp As New DotNetResponse.SQLPersistence
        rp.StoredProcedure = "SP_GET_Producto"
        rp.AddParametro(Id)
        rp.AddParametro("")
        rp.AddParametro("")
        exec.ExecuteDataTable(rp)
        If rp.TieneDatos Then

            lbl_consola.Text = rp.DtTable.Rows(0).Item("Descripcion").ToString()
            lbl_costo.Text = rp.DtTable.Rows(0).Item("costo").ToString()

        End If

    End Sub

    Sub Carga_Forma_Pago()
        Dim exec As New DotNetDal.SQLPersistence(False, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
        Dim rp As New DotNetResponse.SQLPersistence
        rp.StoredProcedure = "SP_GET_CONCEPTO"
        rp.AddParametro("FORMA_PAGO")
        exec.ExecuteDataTable(rp)

        cmb_forma_pago.DisplayMember = "S_TEXTO"
        cmb_forma_pago.ValueMember = "ID_CONCEPTO"
        cmb_forma_pago.DataSource = rp.DtTable

    End Sub
    Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
        Frm_Venta.ShowDialog()
    End Sub

    Private Sub btn_xbox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_maquina1.Click
        Carga_Consola(1, sender.tag)
    End Sub
    Sub Carga_Consola(ByVal Id As Integer, ByVal p_arriendo As String)
        LBL_ID.Text = Id
        lbl_consola.Text = ""
        lbl_inicio_arriendo.Text = "------------------------"
        lbl_termino_arriendo.Text = "------------------------"

        Get_Datos_Producto(Id)
        Carga_Cantidad(1)
        Calcula_Subtotal()
        GetArriendo(Id)


        btn_grabar_e_imprimir.Enabled = False
        Btn_Devolver.Enabled = False
        Btn_Finalizar.Enabled = False

        lbl_p_arriendo.Text = p_arriendo

        If DirectCast(Me.Controls.Find("btn_maquina" & Id, True)(0), Button).BackColor = Color.Red Then
            Btn_Devolver.Enabled = False
            Btn_Finalizar.Enabled = True
        Else
            Btn_Finalizar.Enabled = True
            Btn_Devolver.Enabled = True
        End If

        If Not IsDate(lbl_termino_arriendo.Text) Then
            btn_grabar_e_imprimir.Enabled = True
            Btn_Devolver.Enabled = False
            Btn_Finalizar.Enabled = False
        End If
    End Sub
    Private Sub btn_xbox2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_maquina2.Click
        Carga_Consola(2, sender.tag)
    End Sub
    Private Sub btn_xbox3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_maquina3.Click
        Carga_Consola(3, sender.tag)
    End Sub
    Sub GetArriendo(ByVal id As Integer)
        Dim exec As New DotNetDal.SQLPersistence(False, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
        Dim rp As New DotNetResponse.SQLPersistence
        rp.StoredProcedure = "SP_GET_CONSOLA_ESTADO"
        rp.AddParametro(id)
        exec.ExecuteDataTable(rp)
        If rp.TieneDatos Then
            lbl_inicio_arriendo.Text = rp.DtTable.Rows(0).Item("inicio").ToString()
            lbl_termino_arriendo.Text = rp.DtTable.Rows(0).Item("termino").ToString()
        End If

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Carga_Forma_Pago()
        Load_Maquinas()
    End Sub

    Private Sub cmb_cantidad_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmb_cantidad.SelectedIndexChanged
        Calcula_Subtotal()
    End Sub

    Sub Calcula_Subtotal()
        lbl_sub_total.Text = lbl_costo.Text * cmb_cantidad.SelectedValue
        txt_paga_con.Text = lbl_sub_total.Text
        Calcula_Vuelto()
    End Sub

    Private Sub txt_paga_con_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_paga_con.TextChanged
        Calcula_Vuelto()
    End Sub
    Sub Calcula_Vuelto()
        lbl_vuelto.Text = Val(txt_paga_con.Text) - Val(lbl_sub_total.Text)
    End Sub

   
    Private Sub btn_grabar_e_imprimir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_grabar_e_imprimir.Click
        If IsDate(lbl_termino_arriendo.Text) Then
            If DateDiff(DateInterval.Minute, Now, CDate(lbl_termino_arriendo.Text), FirstDayOfWeek.Monday) > 0 Then
                MsgBox("Maquina en USO")
                Exit Sub
            End If
        End If

        Dim exec As New DotNetDal.SQLPersistence(True, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
        Dim rp As New DotNetResponse.SQLPersistence
        rp.StoredProcedure = "SP_SAVE_CONSOLA_ESTADO"
        rp.AddParametro(Val(LBL_ID.Text))
        rp.AddParametro(Val(cmb_cantidad.SelectedValue))
        rp.AddParametro(Val(lbl_sub_total.Text))
        exec.ExecuteNonQuery(rp)
        exec.Commit()

        If Not rp.Errores Then
            Carga_Consola(Val(LBL_ID.Text), Val(lbl_p_arriendo.Text))

            lbl_consola.Text = "------------------"
            LBL_ID.Text = "0"
            lbl_inicio_arriendo.Text = "----------"
            lbl_termino_arriendo.Text = "----------"
            lbl_sub_total.Text = "0"
            lbl_vuelto.Text = 0
            txt_paga_con.Text = "0"

            Load_Maquinas()

            Btn_Devolver.Enabled = False
            Btn_Finalizar.Enabled = False
            btn_grabar_e_imprimir.Enabled = False

        End If
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Load_Maquinas()
    End Sub
    Sub Load_Maquinas()
        Dim exec As New DotNetDal.SQLPersistence(False, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
        Dim rp As New DotNetResponse.SQLPersistence
        rp.StoredProcedure = "SP_GET_CONSOLAS_ESTADO"
        exec.ExecuteDataTable(rp)
        Dim i As Integer

        For i = 0 To 8
            DirectCast(Me.Controls.Find("btn_maquina" & i + 1, True)(0), Button).Tag = 0
            DirectCast(Me.Controls.Find("btn_maquina" & i + 1, True)(0), Button).Text = "00:00"
            DirectCast(Me.Controls.Find("lbl_termino_maquina" & i + 1, True)(0), Label).Text = "00:00"
            DirectCast(Me.Controls.Find("btn_maquina" & i + 1, True)(0), Button).BackColor = Color.Green
        Next

        If rp.TieneDatos Then
            For i = 0 To rp.DtTable.Rows.Count - 1
                Dim id_Consola As Integer = rp.DtTable.Rows(i).Item("k_producto").ToString()

                DirectCast(Me.Controls.Find("btn_maquina" & id_Consola, True)(0), Button).Tag = rp.DtTable.Rows(i).Item("p_arriendo").ToString()
                DirectCast(Me.Controls.Find("btn_maquina" & id_Consola, True)(0), Button).Text = "00:00"
                DirectCast(Me.Controls.Find("lbl_termino_maquina" & id_Consola, True)(0), Label).Text = "00:00"

                ControlEjecucion(rp, id_Consola, i, "btn_maquina", "lbl_termino_maquina")
            Next
        End If
    End Sub
    Sub ControlEjecucion(ByRef rp As DotNetResponse.SQLPersistence, ByVal id_consola As Integer, ByVal i As Integer, ByVal consola As String, ByVal labelconsola As String)
        Dim tiempo As String = DateDiff(DateInterval.Minute, Now, CDate(rp.DtTable.Rows(i).Item("termino").ToString()))
        Dim horas As String
        Dim negativo As String = ""

        DirectCast(Me.Controls.Find(consola & id_consola, True)(0), Button).BackColor = Color.Green

        If tiempo <= 0 Then
            tiempo = -1 * tiempo
            negativo = "-"
            DirectCast(Me.Controls.Find(consola & id_consola, True)(0), Button).BackColor = Color.Red
        End If

        Dim iHours As String
        Dim iMinutes As String


        iHours = (tiempo Mod 1440) \ 60
        iMinutes = (tiempo Mod 1440) Mod 60
        If iHours.Length = 1 Then iHours = "0" & iHours
        If iMinutes.Length = 1 Then iMinutes = "0" & iMinutes

        horas = iHours & ":" & iMinutes

        If tiempo <= 10 And negativo = "" Then
            DirectCast(Me.Controls.Find(consola & id_consola, True)(0), Button).BackColor = Color.Orange
        End If

        If tiempo > 10 And negativo = "" Then
            DirectCast(Me.Controls.Find(consola & id_consola, True)(0), Button).BackColor = Color.Yellow
        End If


        DirectCast(Me.Controls.Find(consola & id_consola, True)(0), Button).Text = negativo & horas
        DirectCast(Me.Controls.Find(labelconsola & id_consola, True)(0), Label).Text = rp.DtTable.Rows(i).Item("termino").ToString().Substring(11, 5)


    End Sub

    Private Sub btn_maquina4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_maquina4.Click
        Carga_Consola(4, sender.tag)
    End Sub

    Private Sub btn_maquina5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_maquina5.Click
        Carga_Consola(5, sender.tag)
    End Sub

    Private Sub btn_maquina6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_maquina6.Click
        Carga_Consola(6, sender.tag)
    End Sub

    Private Sub btn_maquina7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_maquina7.Click
        Carga_Consola(7, sender.tag)
    End Sub

    Private Sub btn_maquina8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_maquina8.Click
        Carga_Consola(8, sender.tag)
    End Sub

    Private Sub btn_maquina9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_maquina9.Click
        Carga_Consola(9, sender.tag)
    End Sub

    Private Sub Btn_Finalizar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Finalizar.Click
        If MsgBox("¿Confirma Termino?", MsgBoxStyle.YesNo, "Finalizar") = MsgBoxResult.No Then
            Exit Sub
        End If     

        Dim exec As New DotNetDal.SQLPersistence(True, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
        Dim rp As New DotNetResponse.SQLPersistence
        rp.AddParametro(lbl_p_arriendo.Text)
        rp.StoredProcedure = "SP_FINALIZA_CONSOLA_ESTADO"
        exec.ExecuteNonQuery(rp)
        exec.Commit()

        If rp.Errores Then
            MsgBox(rp.MensajeError)

        Else
            lbl_consola.Text = "------------------"
            LBL_ID.Text = "0"
            lbl_inicio_arriendo.Text = "----------"
            lbl_termino_arriendo.Text = "----------"
            lbl_sub_total.Text = "0"
            lbl_vuelto.Text = 0
            txt_paga_con.Text = "0"

            Btn_Devolver.Enabled = False
            Btn_Finalizar.Enabled = False
            btn_grabar_e_imprimir.Enabled = False

            Load_Maquinas()


        End If


    End Sub

   
    Private Sub Btn_Devolver_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Devolver.Click

        If MsgBox("¿Confirma DEVOLVER ANTES DE TIEMPO?", MsgBoxStyle.YesNo, "DEVOLVER") = MsgBoxResult.No Then
            Exit Sub
        End If

        Dim MinutiosRestantes As Integer = 0

        MinutiosRestantes = DateDiff(DateInterval.Minute, Now, CDate(lbl_termino_arriendo.Text))


        Dim DineroRestante As Decimal = Int(MinutiosRestantes / 15) * Val(lbl_costo.Text)

        If MsgBox("¿DEVOLVER $ " & DineroRestante.ToString, MsgBoxStyle.YesNo, "DEVOLVER") = MsgBoxResult.No Then
            Exit Sub
        End If


        Dim exec As New DotNetDal.SQLPersistence(True, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
        Dim rp As New DotNetResponse.SQLPersistence
        rp.AddParametro(lbl_p_arriendo.Text)
        rp.AddParametro(DineroRestante)
        rp.StoredProcedure = "SP_DEVOLVER_CONSOLA_ESTADO"
        exec.ExecuteNonQuery(rp)
        exec.Commit()

        If rp.Errores Then
            MsgBox(rp.MensajeError)

        Else
            lbl_consola.Text = "------------------"
            LBL_ID.Text = "0"
            lbl_inicio_arriendo.Text = "----------"
            lbl_termino_arriendo.Text = "----------"
            lbl_sub_total.Text = "0"
            lbl_vuelto.Text = 0
            txt_paga_con.Text = "0"

            Btn_Devolver.Enabled = False
            Btn_Finalizar.Enabled = False
            btn_grabar_e_imprimir.Enabled = False

            Load_Maquinas()


        End If

    End Sub
End Class
